package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.DictData;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.DictDataService;
import com.tbit.uqbike.service.business.DictTypeService;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import com.tbit.uqbike.webmanager.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author jing
 */
@RestController
@RequestMapping("/dictData")
public class DictDataController extends BaseController {
    @Autowired
    private DictDataService dictDataService;

    @Autowired
    private DictTypeService dictTypeService;

    @GetMapping("/list")
    public JsonResult list(DictData dictData) {
        startPage();
        List<DictData> list = dictDataService.selectDictDataList(dictData);
        return JsonResult.ok(list, (int) new PageInfo<>(list).getTotal());
    }


    /**
     * 查询字典数据详细
     */
    @GetMapping(value = "/{dictCode}")
    public JsonResult getInfo(@PathVariable Integer dictCode) {
        return JsonResult.ok(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public JsonResult dictType(@PathVariable String dictType) {
        List<DictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<DictData>();
        }
        return JsonResult.ok(data);
    }

    /**
     * 新增字典类型
     */
    @PostMapping
    @OperateLog(module = ModuleEnum.DICTDATA, methods = MethodEnum.DICTDATA_ADD)
    public JsonResult add(@Validated @RequestBody DictData dict) {
        return JsonResult.toAjax(dictDataService.insertDictData(dict), getLogin(ServletUtils.getRequest()).context);
    }

    /**
     * 修改保存字典类型
     */
    @PutMapping
    @OperateLog(module = ModuleEnum.DICTDATA, methods = MethodEnum.DICTDATA_EDIT)
    public JsonResult edit(@Validated @RequestBody DictData dict) {
        return JsonResult.toAjax(dictDataService.updateDictData(dict), getLogin(ServletUtils.getRequest()).context);
    }

    /**
     * 删除字典类型
     */
    @DeleteMapping("/{dictCodes}")
    @OperateLog(module = ModuleEnum.DICTDATA, methods = MethodEnum.DICTDATA_DEL)
    public JsonResult remove(@PathVariable Integer[] dictCodes) {
        return JsonResult.toAjax(dictDataService.deleteDictDataByIds(dictCodes), getLogin(ServletUtils.getRequest()).context);
    }
}
